package com.hna.eking.ZkUtils;

import java.io.IOException;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class ZkOptCtx {
	private static final Logger logger = LoggerFactory.getLogger(ZkOptCtx.class);
	private int SESSION_TIMEOUT = 5000; 
	protected String hosts = "127.0.0.1:2181"; 
	private String SessionID;
	private String UUID;
	private String FilePath;
	private String rstFilePath;
	private String HadoopPath;
	private int version;
	
	public final static String PLT_STATUS_RUN  = "0001";
	public final static String PLT_STATUS_SUC  = "0002";
	public final static String PLT_STATUS_FAIL  = "0003";
	private String RootPath = "SynPath";
	
	public ZooKeeper zk;
	
	public ZkOptCtx() throws IOException{
//		hosts = Config.getZookeeper_host();
//		SESSION_TIMEOUT = Integer.valueOf( Config.getPlat_loop_wait_timeout() );
//		HadoopPath = Config.getHadoop_file_path();
//		FilePath.format("/%s/%s/%s.sts", HadoopPath, SessionID, UUID);
//		rstFilePath.format("/%s/%s/%s.ret", HadoopPath, SessionID, UUID);
//		logger.debug("result file :" + FilePath);
		zk = new ZooKeeper(hosts, SESSION_TIMEOUT, new ZkWatcher());
	}
	public ZkOptCtx(String SessionID, String UUID) throws IOException{
		this.SessionID = SessionID;
		this.UUID = UUID;
		
//		hosts = Config.getZookeeper_host();
//		SESSION_TIMEOUT = Integer.valueOf( Config.getPlat_loop_wait_timeout());
//		HadoopPath = Config.getHadoop_file_path();
		FilePath = String.format("/%s/%s/%s", HadoopPath, SessionID, UUID);
		logger.debug("result file :" + FilePath);
		zk = new ZooKeeper(hosts, SESSION_TIMEOUT, null);
		
	}

	
	public void SetEnd(String St){
		try {
			FilePath = String.format("/%s", RootPath);
			logger.debug("file:" + FilePath);
			Stat st = zk.exists(FilePath, false);
			if(st == null){
				logger.debug("file " + FilePath + "not exists");
				zk.create(FilePath, PLT_STATUS_RUN.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 
				FilePath = String.format("/%s/%s", RootPath, SessionID);
				st = zk.exists(FilePath, false);
				if(st == null){
					zk.create(FilePath, PLT_STATUS_RUN.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 
				}
				FilePath = String.format("/%s/%s/%s", RootPath, SessionID, UUID);
				st = zk.exists(FilePath, false);
				if(st == null){
					zk.create(FilePath, PLT_STATUS_RUN.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 
				}
			}
			System.out.println("result path is:" + FilePath);
			zk.setData(FilePath, St.getBytes(), -1);
		} catch (KeeperException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void CloseZK() throws InterruptedException{
		zk.close();
	}
	public int getSESSION_TIMEOUT() {
		return SESSION_TIMEOUT;
	}
	public void setSESSION_TIMEOUT(int sESSION_TIMEOUT) {
		SESSION_TIMEOUT = sESSION_TIMEOUT;
	}
	public String getHosts() {
		return hosts;
	}
	public void setHosts(String hosts) {
		this.hosts = hosts;
	}
	public String getSessionID() {
		return SessionID;
	}
	public void setSessionID(String sessionID) {
		SessionID = sessionID;
	}
	public String getUUID() {
		return UUID;
	}
	public void setUUID(String uUID) {
		UUID = uUID;
	}
	public String getFilePath() {
		return FilePath;
	}
	public void setFilePath(String filePath) {
		FilePath = filePath;
	}
	public String getRstFilePath() {
		return rstFilePath;
	}
	public void setRstFilePath(String rstFilePath) {
		this.rstFilePath = rstFilePath;
	}
	public String getHadoopPath() {
		return HadoopPath;
	}
	public void setHadoopPath(String hadoopPath) {
		HadoopPath = hadoopPath;
	}
	
}
